/**
 * 滚动加载
 * @param {obj} scrollTarget  目标滚动区域对象
 * @param {fn} scrollDownCallback 滚动回调
 */
function scrollLoad(scrollTarget, scrollDownCallback) {
    //大分辨率，20条数据不足以出现滚动条时，用鼠标滚轮时间辅助加载更多数据
    scrollTarget.addEventListener('mousewheel', ev => {
        ev = ev || window.event;
        if (ev.wheelDelta < 0 && scrollTarget.scrollHeight <= scrollTarget.clientHeight) { //向下滚动 且 不存在滚动条时。 
            scrollDownCallback();
        }
    })
    //兼容火狐滚轮事件 
    scrollTarget.addEventListener('DOMMouseScroll', (ev) => {
        ev = ev || window.event;
        if (ev.detail > 0 && scrollTarget.scrollHeight <= scrollTarget.clientHeight) {
            scrollDownCallback();
        }
    });

    let preScrollTop = 0;
    scrollTarget.addEventListener('scroll', () => {

        let t = scrollTarget.scrollTop
        let h = scrollTarget.scrollHeight
        let bh = scrollTarget.clientHeight

        let onOff = null;
        if (t - preScrollTop > 0) { //向下滚动
            onOff = false;
        } else {
            onOff = true;
        }

        preScrollTop = t;
        if (
            !onOff
            && h - bh - t < 30
        ) {
            scrollDownCallback();
        }
    })
}

//表格和卡片列表页面滚动加载封装
function tableAndCardListScrollLoad(scrollDownCallback) {
    Array.from(document.querySelectorAll('.u-table-body')).forEach((scrollTarget) => {
        scrollLoad(scrollTarget, scrollDownCallback);
    })
}

//缩略滚动加载封装
function thumbListScrollLoad(scrollDownCallback) {
    Array.from(document.querySelectorAll('.ssc-row-thumb-scroll-mark')).forEach((scrollTarget) => {
        scrollLoad(scrollTarget, scrollDownCallback);
    })
}



export { tableAndCardListScrollLoad, scrollLoad, thumbListScrollLoad }